Action.adjust = (function(){
    /*****************缩放**************/
    var resizeInput;

    /*****************剪切**************/
    var clip = {
        left: 350,
        top: 200,
        width: 100,
        height: 100,
        fix:[[6,5],[5,5],[5,4],[6,4]]
    };
    var clipBorder,
        nwClipHandler,
        neClipHandler,
        seClipHandler,
        swClipHandler,
        clipInputWidth,
        clipInputHeight,
        clipInputScale;

    function init(){
        /***********************常用调整*************************************/

        $('#adjSlider').hide();
        /*
        $("#toolBox>div>div:first-of-type li:first-of-type").click(attAdj);
        
        $('#adjSlider>div:nth-of-type(2)').slider({
            min:-150,
            max:150,
            value:0,
            stop:function(event,ui){
            },
            slide:function(event,ui){
                $('#lightValue').empty().append(ui.value);
            }
        });

        $('#adjSlider>div:nth-of-type(5)').slider({
            min:-50,
            max:100,
            value:0,
            stop:function(event,ui){
            },
            slide:function(event,ui){
                $('#compareValue').empty().append(ui.value);
            }
        });
        $('#adjSlider>div:nth-of-type(8)').slider({
            min:-100,
            max:100,
            value:0,
            stop:function(event,ui){
            },
            slide:function(event,ui){
                $('#saturateValue').empty().append(ui.value);
            }
        });
        $('#adjSlider span').click(function(){
            $('#adjSlider').fadeOut();
        });
        */


        /***********************缩放*************************************/
        resizeInput = $("#resizer input");
        $("#toolBox>div>div:first-of-type li:nth-of-type(1)").click(sizeAdj);
        $('#resizer').hide();
        $('#resizer span.x-cross').click(function(){                
            $('#resizer').fadeOut();
        });
        $('#resizer button').click(doSizeAdj);
        $(resizeInput[0]).bind('change',function(){
            if(resizeInput[2].checked){
                var v = parseInt(resizeInput[0].value);
                resizeInput[1].value = Math.round(PP.h*v/PP.w);
            }
        });
        $(resizeInput[1]).bind('change',function(){
            if(resizeInput[2].checked){
                var v = parseInt(resizeInput[1].value);
                resizeInput[0].value = Math.round(PP.w*v/PP.h);
            }
        });

        /***********************剪切*************************************/
        clipBorder = $("#cliperBox>div:first-of-type");
        nwClipHandler = $("#cliperBox>div:nth-of-type(2)");
        neClipHandler = $("#cliperBox>div:nth-of-type(3)"),
        seClipHandler = $("#cliperBox>div:nth-of-type(4)"),
        swClipHandler = $("#cliperBox>div:nth-of-type(5)");
        clipInputWidth = $("#cliper input:first-of-type")[0];
        clipInputHeight = $("#cliper input:nth-of-type(2)")[0];
        clipInputScale = $("#cliper input:nth-of-type(3)")[0];

        $(clipInputWidth).bind('change',function(){
            if(clipInputScale.checked){
                var v = parseInt(clipInputWidth.value);
                
                clip.height = clipInputHeight.value = Math.round(clip.height*v/clip.width);
                clip.width = v;
            }
            inputChangeCliper();
        });

        $(clipInputHeight).bind('change',function(){
            if(clipInputScale.checked){
                var v = parseInt(clipInputHeight.value);
                
                clip.width = clipInputWidth.value = Math.round(clip.width*v/clip.height);
                clip.height = v; 
            }
            inputChangeCliper();
        });

        $("#toolBox>div>div:first-of-type li:nth-of-type(2)").click(clipAdj);
        $('#cliper').hide();
        $('#cliperBox>div').hide();
        $('#cliper span.x-cross').click(function(){                
            $('#cliper').fadeOut();
            $('#cliperBox>div').fadeOut();
        });
        $('#cliper button').click(doClipAdj);
        
        clipBorder.draggable({ 
            containment: '#canvasBox',
            drag : moveClipBorder
        });
        
        
        nwClipHandler.draggable({
            containment: '#canvasBox',
            drag : nwChangeCliper
        });
        neClipHandler.draggable({
            containment: '#canvasBox',
            drag : neChangeCliper
        });
        
        seClipHandler.draggable({
            containment: '#canvasBox',
            drag : seChangeCliper
        });
        swClipHandler.draggable({
            containment: '#canvasBox',
            drag : swChangeCliper
        });

    }
/*=========================公用逻辑============================================*/
    function showAdj(type){
        if(!PP.isPicLoad){
            Action.work.openAlbum();
            return;
        }
        switch(type){
            case 'att':
                $('#cliperBox>div').fadeOut();
                $('#cliper').fadeOut();
                $('#resizer').fadeOut();
                $('#adjSlider').fadeIn();
                break;
            case 'resize':
                $('#cliperBox>div').fadeOut();
                $('#cliper').fadeOut();
                $('#adjSlider').fadeOut();
                $('#resizer').fadeIn();
                break;
            case 'clip':
                $('#adjSlider').fadeOut();
                $('#resizer').fadeOut();
                $('#cliperBox>div').fadeIn();
                $('#cliper').fadeIn();
                break;
        }
    }
/*=========================裁剪逻辑============================================*/    
    function moveClipBorder(event,ui){
        var x = ui.position.left,
            y = ui.position.top;
        clip.left = x;
        clip.top = y;

        nwClipHandler[0].style.left = (x - clip.fix[0][0])+"px";
        nwClipHandler[0].style.top = (y - clip.fix[0][1])+"px";
        neClipHandler[0].style.left = (x + clip.width - clip.fix[1][0])+"px";
        neClipHandler[0].style.top = (y - clip.fix[1][1])+"px";
        seClipHandler[0].style.left = (x + clip.width - clip.fix[2][0])+"px";
        seClipHandler[0].style.top = (y + clip.height - clip.fix[2][1])+"px";
        swClipHandler[0].style.left = (x - clip.fix[0][0])+"px";
        swClipHandler[0].style.top = (y + clip.height - clip.fix[0][1])+"px";
    }

    function nwChangeCliper(event,ui){
        event.stopPropagation()
        var newPos = ui.position,
            x = newPos.left + clip.fix[0][0],
            y = newPos.top + clip.fix[0][1],
            dx = x - clip.left,
            dy = y - clip.top,
            w = clip.width - dx,
            h = clip.height - dy;
        if(w>20 && h>20){
            clip.left = x;
            clip.top = y;
            clip.width = w;
            clip.height = h;
            clipBorder[0].style.left = x+"px";
            clipBorder[0].style.top = y+"px"; 
            clipBorder[0].style.width = w+'px';
            clipBorder[0].style.height = h+'px';

            neClipHandler[0].style.top =  newPos.top+"px";
            swClipHandler[0].style.left =  newPos.left+"px";

            clipInputWidth.value = w;
            clipInputHeight.value = h;
        }
    }

    function neChangeCliper(event,ui){
        event.stopPropagation()
        var newPos = ui.position,
            x = newPos.left + clip.fix[1][0],
            y = newPos.top + clip.fix[1][1],
            dy = y - clip.top,
            w = x - clip.left,
            h = clip.height - dy;
        if(w>20 && h>20){
            clip.top = y;
            clip.width = w;
            clip.height = h;
            clipBorder[0].style.top = y+"px"; 
            clipBorder[0].style.width = w+'px';
            clipBorder[0].style.height = h+'px';

            nwClipHandler[0].style.top =  newPos.top+"px";
            seClipHandler[0].style.left =  newPos.left+"px";

            clipInputWidth.value = w;
            clipInputHeight.value = h;
        }
    }

    function seChangeCliper(event,ui){
        event.stopPropagation()
        var newPos = ui.position,
            w = newPos.left + clip.fix[2][0] - clip.left,
            h = newPos.top + clip.fix[2][1] - clip.top;
        if(w>20 && h>20){
            clip.width = w;
            clip.height = h;
            clipBorder[0].style.width = w+'px';
            clipBorder[0].style.height = h+'px';

            neClipHandler[0].style.left =  newPos.left+"px";
            swClipHandler[0].style.top =  newPos.top+"px";

            clipInputWidth.value = w;
            clipInputHeight.value = h;
        }
    }

    function swChangeCliper(event,ui){
        event.stopPropagation()
        var newPos = ui.position,
            x = newPos.left + clip.fix[3][0],
            y = newPos.top + clip.fix[3][1],
            dx = x - clip.left,
            //dy = y - clip.top,
            w = clip.width - dx;
            h = y-clip.top;
        if(w>20 && h>20){
            clip.left = x;
            clip.width = w;
            clip.height = h;
            clipBorder[0].style.left = x+"px";
            clipBorder[0].style.width = w+'px';
            clipBorder[0].style.height = h+'px';

            nwClipHandler[0].style.left =  newPos.left+"px";
            seClipHandler[0].style.top =  newPos.top+"px";

            clipInputWidth.value = w;
            clipInputHeight.value = h;
        }
    }

    function inputChangeCliper(){
        if(clip.left+clip.width > PP.WIDTH){
            clip.left = PP.WIDTH - clip.width;
        }
        if(clip.top+clip.height > PP.HEIGHT){
            clip.top = PP.HEIGHT - clip.height;
        }

        clipBorder[0].style.left = clip.left+"px";
        clipBorder[0].style.top = clip.top+"px";
        clipBorder[0].style.width = clip.width+'px';
        clipBorder[0].style.height = clip.height+'px';

        nwClipHandler[0].style.left =  (clip.left - clip.fix[0][0])+"px";
        nwClipHandler[0].style.top =  (clip.top - clip.fix[0][1])+"px";

        neClipHandler[0].style.left =  (clip.left + clip.width - clip.fix[1][0])+"px";
        neClipHandler[0].style.top =  (clip.top - clip.fix[1][1])+"px";

        seClipHandler[0].style.left =  (clip.left + clip.width - clip.fix[2][0])+"px";
        seClipHandler[0].style.top =  (clip.top + clip.height - clip.fix[2][1])+"px";

        swClipHandler[0].style.left =  (clip.left - clip.fix[3][0])+"px";
        swClipHandler[0].style.top =  (clip.top + clip.height - clip.fix[3][1])+"px";
    }

    function clipAdj(){
		clipInputWidth.value = clip.width;
        clipInputHeight.value = clip.height;

        showAdj('clip');
    }

    function doClipAdj(){
        var nw = clip.width,
            nh = clip.height,
            nx = (PP.WIDTH - nw)/2,
            ny = (PP.HEIGHT - nh)/2,
            input = PP.context.getImageData(clip.left, clip.top, nw, nh);

        PP.context.clearRect(0,0,PP.WIDTH,PP.HEIGHT);

        var cx = document.createElement("canvas");
        cx.width = nw;
        cx.height = nh;
        (cx.getContext("2d")).putImageData(input,0,0);
        PP.context.drawImage(cx,nx,ny,nw,nh);
        $.extend(PP,{
            w : nw,
            h : nh,
            x : nx,
            y : ny
        });

        $('#cliper').fadeOut();
        $('#cliperBox>div').fadeOut();
    }
/*=========================属性修改逻辑============================================*/
    function attAdj(){
        showAdj('att');
    }
/*=========================缩放逻辑============================================*/
    function sizeAdj(){
        resizeInput[0].value = PP.w || "1";
        resizeInput[1].value = PP.h || "1";
        showAdj('resize');
    }

    function doSizeAdj(){
        var size = $('#resizer input'),
            nw = parseInt(size[0].value),
            nh = parseInt(size[1].value),
            nx = (PP.WIDTH - nw)/2,
            ny = (PP.HEIGHT - nh)/2,
            input = PP.context.getImageData(PP.x, PP.y, PP.w, PP.h);

        PP.context.clearRect(0,0,PP.WIDTH,PP.HEIGHT);

        var cx = document.createElement("canvas");
        cx.width = PP.w;
        cx.height = PP.h;
        (cx.getContext("2d")).putImageData(input,0,0);
        PP.context.drawImage(cx,nx,ny,nw,nh);
    }


    return {
        init : init
    };
})();